<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <!-- <template slot="infoForm">
        <avue-crud ref="infoCrud" :option="infoOption" :data="infoData" v-model="infoForm">

        </avue-crud>
      </template> -->
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
      <template slot="menu" slot-scope="{ row }">
        <el-button type="text" @click="viewSequenceNumber(row.id)" icon="el-icon-view" size="small" v-if="permission.iqc_inspected_view">查看序列号</el-button>
      </template>
    </avue-crud>
    <el-dialog title="查看" append-to-body :visible.sync="showViewDialog" width="60%" :fullscreen="fullscreenViewDialog" @close="cancelView">
      <div slot="title">
        查看序列号<i
          class="el-dialog__close fill-screen-icon"
          :class="fullscreenViewDialog ? 'el-icon-news' : 'el-icon-full-screen'"
          @click="fullscreenViewDialog = !fullscreenViewDialog"
        ></i>
      </div>
      <avue-crud ref="infoCrud" :option="infoOption" :data="infoData" v-model="infoForm"> </avue-crud>
    </el-dialog>
  </basic-container>
</template>

<script>
import { getList } from '@/api/warehouse/customer-code-query';
import { mapGetters } from 'vuex';
import userTableConfig from '@/components/user-table-config';
import userConfig from '@/mixins/userConfig';

export default {
  components: {
    userTableConfig
  },
  mixins: [userConfig()],
  data() {
    return {
      pageCode: 'CUSTOMER_CODE_QUERY',
      form: {},
      infoForm: {},
      query: {},
      infoData: [],
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      fullscreenViewDialog: false,
      showViewDialog: false,
      infoOption: {
        selection: true,
        editBtn: false,
        refreshBtn: false,
        border: true,
        addBtn: false,
        delBtn: true,
        menu: false,
        tip: false,
        index: true,
        selection: false,
        indexLabel: '序号',
        columnBtn: false,
        column: [
          {
            label: '序列号',
            prop: 'sequnceNumber'
          }
        ]
      },
      option: {
        height: 'calc(100vh - 440px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        indexLabel: '序号',
        viewBtnText: '查看序列号',
        viewBtn: false,
        addBtn: false,
        editBtn: false,
        columnBtn: false,
        delBtn: false,
        selection: false,
        menu: false,
        dialogClickModal: false,
        column: [
          {
            label: '包装条码',
            prop: 'sequenceNumber',
            search: true,
            viewDisplay: false,
            rules: [
              {
                required: true,
                message: '请输入包装条码',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '客户名称',
            prop: 'customerName',
            search: true,
            viewDisplay: false,
            rules: [
              {
                required: true,
                message: '请输入客户名称',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '客户条码',
            prop: 'externalSequenceNumber',
            search: true,
            viewDisplay: false,
            rules: [
              {
                required: true,
                message: '请输入客户条码',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '产品名称',
            prop: 'partMasterCode',
            viewDisplay: false,
            rules: [
              {
                required: true,
                message: '请输入产品名称',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '产品名称',
            prop: 'partMasterName',
            viewDisplay: false,
            rules: [
              {
                required: true,
                message: '请输入产品名称',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '包装数量',
            prop: 'quantity',
            viewDisplay: false,
            rules: [
              {
                required: true,
                message: '请输入包装数量',
                trigger: 'blur'
              }
            ]
          }
        ]
        // group: [
        //   {
        //     label: '',
        //     prop: 'sequenceNumberInfo',
        //     labelWidth: 0,
        //     width: 0,
        //     column: [
        //       {
        //         label: '',
        //         prop: 'info',
        //         span: 24,
        //         formslot: true
        //       }
        //     ]
        //   }
        // ]
      },
      data: []
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.pallet_add, false),
        viewBtn: this.vaildData(this.permission.pallet_view, false),
        delBtn: this.vaildData(this.permission.pallet_delete, false),
        editBtn: this.vaildData(this.permission.pallet_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  methods: {
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          done();
        },
        (error) => {
          loading();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
        });
    },
    cancelView() {
      this.showViewDialog = false;
    },
    viewSequenceNumber() {
      this.showViewDialog = true;
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (['edit', 'view'].includes(type)) {
        getDetail(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      params.isExSequencePage = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.fill-screen-icon {
  color: #909399;
  font-size: 15px;
  float: right;
  padding-right: 25px;
  &:hover {
    color: #409eff;
  }
}
::v-deep .el-dialog__body {
  .avue-crud__menu {
    display: none;
  }
}
</style>
